package com.baihan.angus.app.game.busshow.ui.main.log_in;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.baihan.angus.app.game.basic.base.BaseActivity;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.databinding.ActivityLoginBinding;
import com.baihan.angus.app.game.busshow.pop_dialog.AgreePrivacyDialog;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.constants.CommonVariable;
import com.gyf.immersionbar.ImmersionBar;
import com.qmuiteam.qmui.span.QMUITouchableSpan;

/**
 * 登录
 */

@Route(path = ArouterAppPath.LOGIN_ACTIVITY)
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginVM> {

    @Autowired(name = "userType")
    int userType;

    private AgreePrivacyDialog mAgreePrivacyDialog;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected int getViewModelId() {
        return BR.loginVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        ImmersionBar.with(this).fitsSystemWindows(false).statusBarColor(R.color.transparent).statusBarDarkFont(false).init();

        mVDBinding.tvQuestion.setOnClickListener(view -> {

        });

        mVDBinding.tvGetCode.setOnClickListener(view -> {

        });

        mVDBinding.tvPhoneLogin.setOnClickListener(view -> {

        });

        mVDBinding.tvWechat.setOnClickListener(view -> {

        });

        mVDBinding.tvQQ.setOnClickListener(view -> {

        });

        mVDBinding.tvTip.setMovementMethodDefault();
        mVDBinding.tvTip.setNeedForceEventToParent(true);
        mVDBinding.tvTip.setText(generateSp(getString(R.string.agreementPrivacyTips)));

        showDialog();
    }

    private void showDialog() {
        if (null == mAgreePrivacyDialog) {
            mAgreePrivacyDialog = new AgreePrivacyDialog(this, new AgreePrivacyDialog.Callback() {
                @Override
                public void onNotUse() {
                    mViewModel.ofAgreePrivacy.set(false);
                    mAgreePrivacyDialog.dismiss();
                }

                @Override
                public void onAgree() {
                    mViewModel.ofAgreePrivacy.set(true);
                    mAgreePrivacyDialog.dismiss();
                }
            });
        }
        mAgreePrivacyDialog.show();
    }

    @Override
    protected void initData() {
        mViewModel.setUserType(userType);

        long timeIndex = CommonVariable.REGISTER_TIME - System.currentTimeMillis();
        if (timeIndex > 0) {
            mViewModel.timeLimit = timeIndex / 1000;
            mViewModel.codeTimeLimit();
            mViewModel.ofCodeEnable.set(false);
        } else {
            mViewModel.ofGetCode.set(getString(R.string.getVerifyCode));
        }
    }
    //初始化 Span
    private SpannableString generateSp(String text) {
        String highlight1 = getString(R.string.agreement);
        String highlight2 = getString(R.string.privacyTips);
        SpannableString sp = new SpannableString(text);
        int start = 0, end;
        int index;
        while ((index = text.indexOf(highlight1, start)) > -1) {
            end = index + highlight1.length();
            sp.setSpan(new QMUITouchableSpan(getColor(R.color.textColor22), getColor(R.color.textColor22), 0, 0) {
                @Override
                public void onSpanClick(View widget) {
//                    ARouter.getInstance().build(ArouterAppPath.SINGLE_PAGE_ACTIVITY).withString("no", "eula").navigation();
                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;

            //加粗
            StyleSpan colorSpan = new StyleSpan(Typeface.BOLD);
            sp.setSpan(colorSpan, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }

        start = 0;
        while ((index = text.indexOf(highlight2, start)) > -1) {
            end = index + highlight2.length();
            sp.setSpan(new QMUITouchableSpan(getColor(R.color.textColor22), getColor(R.color.textColor22), 0, 0) {
                @Override
                public void onSpanClick(View widget) {

                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;

            //加粗
            StyleSpan colorSpan = new StyleSpan(Typeface.BOLD);
            sp.setSpan(colorSpan, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }

        return sp;
    }

}